package gdr.dev;

import java.awt.*;

import gdr.action.core.*;
import gdr.battle.core.*;
import gdr.battle.core.condizioni.*;
import gdr.battle.data.*;
import gdr.battle.data.map.*;
import gdr.battle.data.map.BattleMapUnit.MapUnitType;
import gdr.battle.graphics.*;
import gdr.dev.creator.*;
import gdr.player.*;
import gdr.window.*;

/**
 * Genera casualmente uno scontro
 * @author Giorgio Vitale
 *
 */
public class RandomGenerator
{
	protected BattleContainer bc;
	
	
	public BattleContainer getContainer(SuperGDR9000 gdr){
		bc = new BattleContainer();
		
		//creo e collego i moduli
		BattleManager manager = new BattleManager(gdr);
		DataHolder data = new DataHolder();
		bc.setData(data);
		bc.setManager(manager);
		manager.setContainer(bc);
		
		GestoreTurni gt = new GestoreTurni();
		bc.getData().setGestoreTurni(gt);

		//imposto stato dello scontro
		data.setStatoScontro(StatoScontro.INIZIALIZZAZIONE_SCONTRO);
		
		//creo gestore coordinate
		//carico l'interfaccia grafica del client
		HumanGraphicalClient human = new HumanGraphicalClient(gdr);
		bc.getClients().add(human);
		human.setContainer(bc);
		human.init();
		CoordinateCalculator coordinate = new CoordinateCalculator();
		coordinate.setCenter(new Position(3,3,1));
		coordinate.setGame(gdr);
		human.setCoordinate(coordinate);
		human.setBackground(new BaseBackground(gdr));
		
		//creazione e collegamento mappa 
		BattleMap m = MapCreator.createBridgeMap(bc, human, 14, 14, 1, 6);
		data.setMap(m);
		
		//creazione gestore azioni
		ActionManager actionManager = new ActionManager(gdr);
		data.setActionManager(actionManager);
		bc.getManager().addChild(actionManager);
		actionManager.setContainer(bc);
		
		//creazione team
		Team team1 = TeamCreator.createTeam(bc, "Player", Color.green);
		team1.setCondizioneVincita(new CondizioneVincitaDeathMatch(bc, team1));
		team1.setCondizionePerdita(new CondizionePerditaSconfittaTotale(bc, team1));
		human.setClientTeam(team1);
		
		Team team2 = TeamCreator.createTeam(bc, "Enemy", Color.red);
		team2.setCondizioneVincita(new CondizioneVincitaDeathMatch(bc, team2));
		team2.setCondizionePerdita(new CondizionePerditaSconfittaTotale(bc, team2));
		CPUClient cpu2 = TeamCreator.createCPUClient(bc, team2);
		bc.getClients().add(cpu2);
		
		Team team3 = TeamCreator.createTeam(bc, "Ally", Color.cyan);
		team3.setCondizioneVincita(new CondizioneVincitaDeathMatch(bc, team3));
		team3.setCondizionePerdita(new CondizionePerditaSconfittaTotale(bc, team3));
		CPUClient cpu3 = TeamCreator.createCPUClient(bc, team3);
		bc.getClients().add(cpu3);
		
		Team team4 = TeamCreator.createTeam(bc, "Neutral", Color.pink);
		team4.setCondizioneVincita(new CondizioneVincitaImpossibile());
		team4.setCondizionePerdita(new CondizionePerditaSconfittaTotale(bc, team4));
		CPUClient cpu4 = TeamCreator.createCPUClient(bc, team4);
		bc.getClients().add(cpu4);
		
		team1.getEnemy().add(team2);
		team2.getEnemy().add(team1);
		
		team1.getAlly().add(team3);
		team3.getAlly().add(team1);
		
		team2.getEnemy().add(team3);
		team3.getEnemy().add(team2);
		
		for (int i=0; i<m.getWidth(); i++)
			for (int j=0; j<m.getHeight(); j++)
				if (m.getBattleMapUnit(new Position(i, j, 1)).getMapUnitType() == MapUnitType.GATE_UNIT){
					BattleMapUnit u = m.getBattleMapUnit(new Position(i,j,1));
					BattleMapUnitGate gate = (BattleMapUnitGate)u;
					gate.setOwner(team1);
				}
		
		Area area = new Area(0, 0, 4, 4);
		/*DefaultPlayer pp = OnMapPlayerCreator.createGraphicalPlayer(gdr, bc, false, area, human, human, coordinate, team1, PlayerCreator.createTank(gdr, "A", 100));
		pp.setEsperienzaFisica(pp.getLevel()*5*2);
		OnMapPlayerCreator.createGraphicalPlayer(gdr, bc, false, area, human, human, coordinate, team1, PlayerCreator.createTank(gdr, "B", 100));
		pp = OnMapPlayerCreator.createGraphicalPlayer(gdr, bc, false, area, human, human, coordinate, team1, PlayerCreator.createWizard(gdr, "C", 100));
		pp.setEsperienzaMagica(pp.getLevel()*2*8);
		OnMapPlayerCreator.createGraphicalPlayer(gdr, bc, false, area, human, human, coordinate, team1, PlayerCreator.createWizard(gdr, "D", 100));
		OnMapPlayerCreator.createGraphicalPlayer(gdr, bc, false, area, human, human, coordinate, team1, PlayerCreator.createSummoner(gdr, "E", 100));
		*/
		OnMapPlayerCreator.createGraphicalPlayer(gdr, bc, false, area, human, human, coordinate, team1, PlayerCreator.createSummoner(gdr, "F", 100));
		OnMapPlayerCreator.createGraphicalPlayer(gdr, bc, false, area, human, human, coordinate, team1, PlayerCreator.createSummoner(gdr, "G", 100));
		OnMapPlayerCreator.createGraphicalPlayer(gdr, bc, false, area, human, human, coordinate, team1, PlayerCreator.createHealer(gdr, "H", 100));
		
		area = new Area(bc.getData().getMap().getWidth()-4, 0, 4, 8);
		for (int i=0; i<20; i++){
			OnMapPlayerCreator.createGraphicalPlayer(gdr, bc, true, area, human, cpu2, coordinate, team2, PlayerCreator.createTank(gdr, "CC", 100));
			OnMapPlayerCreator.createGraphicalPlayer(gdr, bc, true, area, human, cpu2, coordinate, team2, PlayerCreator.createWizard(gdr, "DD", 100));
		}
		
		area = new Area(0, 4, 4, 8);
		for (int i=0; i<20; i++){
			OnMapPlayerCreator.createGraphicalPlayer(gdr, bc, true, area, human, cpu3, coordinate, team3, PlayerCreator.createTank(gdr, "CC", 9999));
			OnMapPlayerCreator.createGraphicalPlayer(gdr, bc, true, area, human, cpu3, coordinate, team3, PlayerCreator.createWizard(gdr, "DD", 9999));
		}
			
		area = new Area(0, 0, 8, 4);
		OnMapPlayerCreator.createGraphicalPlayer(gdr, bc, true, area, human, cpu4, coordinate, team4, PlayerCreator.createTank(gdr, "dag", 100));
		return bc;
	}
}
